package com.kanshu.ksgb.fastread.doudou.mfdzsb.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.common.business.pay.event.PayActionEvent;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;

/**
 * 微信支付回调activity
 * Created by wcy on 2017\5\23 0023.
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI mIWXAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String wxId = getResources().getString(R.string.wx_app_id);
        mIWXAPI = WXAPIFactory.createWXAPI(this, wxId);
        mIWXAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        mIWXAPI.handleIntent(intent, this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        PayActionEvent event = new PayActionEvent();
        if (baseResp != null && baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
                event.isSuccess = true;
            } else {
                event.isSuccess = false;
            }
            /*BaseResp.ErrCode.ERR_AUTH_DENIED;
            BaseResp.ErrCode.ERR_COMM;
            BaseResp.ErrCode.ERR_SENT_FAILED;
            BaseResp.ErrCode.ERR_UNSUPPORT;
            BaseResp.ErrCode.ERR_USER_CANCEL;*/
            event.errCode = baseResp.errCode;
            event.errDesc = baseResp.errStr;
            // 发送消息
            EventBus.getDefault().post(event);
            finish();
        }
    }
}
